/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.equipment.web;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Result;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.equipment.entity.EleEquipment;
import com.thinkgem.jeesite.modules.equipment.service.EleEquipmentService;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.OfficeService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 设备管理Controller
 * @author ZC
 * @version 2017-04-18
 */
@Controller
@RequestMapping(value = "${adminPath}/equipment/eleEquipment")
public class EleEquipmentController extends BaseController {

    @Autowired
    private OfficeService officeService;
	@Autowired
	private EleEquipmentService eleEquipmentService;
	
	@ModelAttribute
	public EleEquipment get(@RequestParam(required=false) String id) {
		EleEquipment entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eleEquipmentService.get(id);
		}
		if (entity == null){
			entity = new EleEquipment();
		}
		return entity;
	}
	
	@RequiresPermissions("equipment:eleEquipment:view")
	@RequestMapping(value = {"list", ""})
	public String list(EleEquipment eleEquipment, Model model) {
        List<Office> listTree = new ArrayList<>();

        User thisuser = UserUtils.getUser();
        String parentId = thisuser.getCompany().getId();
        String parentIdsStr = thisuser.getCompany().getParentIds();

        Office office = new Office();
        List<Office> list = officeService.findList(office);
        for(Office item :list){
            if((parentIdsStr.contains(item.getId()+",") && !parentIdsStr.equals(item.getParentIds()))
                    || (item.getParentIds().contains(parentIdsStr) && !parentIdsStr.equals(item.getParentIds()))
                    || parentId.equals(item.getId())){
                item.setAddress(null);
                item.setEmail(null);
                item.setFax(null);
                item.setRemarks(null);
                item.setPhone("display:none;");
                listTree.add(item);
            }
        }
        List<EleEquipment> listEquipment = eleEquipmentService.findList(eleEquipment);
        for(EleEquipment equipment:listEquipment){
            Office Office = new Office();
            Office.setId(equipment.getId());
            Office.setParent(new Office(equipment.getOffice().getId()));
            Office.setAddress(equipment.getCode());
            Office.setEmail(equipment.getType().getModel());
            Office.setFax(equipment.getWidth()+" X "+equipment.getHeight());
            Office.setRemarks(equipment.getRemarks());
            Office.setPhone("");
            listTree.add(Office);
        }
		model.addAttribute("list", listTree);
		return "modules/equipment/eleEquipmentList";
	}

	@RequiresPermissions("equipment:eleEquipment:view")
	@RequestMapping(value = "form")
	public String form(EleEquipment eleEquipment, Model model) {
        User thisuser = UserUtils.getUser();
        eleEquipment.setCompany(thisuser.getCompany());
        if (!UserUtils.getSubject().isPermitted("sys:eleEquipment:check")){
            eleEquipment.setOffice(thisuser.getOffice());
        }
		model.addAttribute("eleEquipment", eleEquipment);
		return "modules/equipment/eleEquipmentForm";
	}

	@RequiresPermissions("equipment:eleEquipment:edit")
	@RequestMapping(value = "save")
	public String save(EleEquipment eleEquipment, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, eleEquipment)){
			return form(eleEquipment, model);
		}
        User thisuser = UserUtils.getUser();
        eleEquipment.setCreateBy(UserUtils.getUser());
        eleEquipment.setCheckDate(new Date());
        if (!UserUtils.getSubject().isPermitted("sys:eleEquipment:check")){
            eleEquipment.setCheckBy(thisuser);
            eleEquipment.setDelFlag(EleEquipment.DEL_FLAG_NORMAL);
        }else{
            eleEquipment.setCheckBy(UserUtils.getCheckUser());
            eleEquipment.setDelFlag(EleEquipment.DEL_FLAG_AUDIT);
        }
		UserUtils.putCache(UserUtils.getUser().getId()+"_CREATE_EQUIPMENT", eleEquipment);
		return "modules/equipment/eleEquipmentInfo";
	}

	@RequiresPermissions("equipment:eleEquipment:edit")
	@RequestMapping(value = "formsave")
	public String formsave(Model model) {
		EleEquipment equipment = (EleEquipment)UserUtils.getCache(UserUtils.getUser().getId()+"_CREATE_EQUIPMENT");
		// 保存设备信息
        try{
            eleEquipmentService.save(equipment);
            model.addAttribute("result", "success");
        }catch(Exception e){
            logger.error("保存设备信息失败:", e);
            model.addAttribute("result", "fail");
        }
        model.addAttribute("equipment", equipment);
		UserUtils.removeCache(UserUtils.getUser().getId()+"_CREATE_EQUIPMENT");
		return "modules/equipment/eleEquipmentInfoConfirmTips";
	}
	
	@RequiresPermissions("equipment:eleEquipment:edit")
	@RequestMapping(value = "delete")
	public String delete(EleEquipment eleEquipment, RedirectAttributes redirectAttributes) {
		eleEquipmentService.delete(eleEquipment);
		addMessage(redirectAttributes, "删除设备成功");
		return "redirect:"+Global.getAdminPath()+"/equipment/eleEquipment/?repage";
	}

	@RequiresPermissions("sys:eleEquipment:edit")
	@RequestMapping(value = "recheck")
	public String recheck(String id, RedirectAttributes redirectAttributes){
		EleEquipment equipment = new EleEquipment(id);
		equipment.setDelFlag(User.DEL_FLAG_AUDIT);
		Result result = eleEquipmentService.checkEquipment(equipment);
		addMessage(redirectAttributes, "已经重新提交审核!");
		return "redirect:" + adminPath + "/equipment/eleEquipment/info?id=" + id;
	}

	@RequiresPermissions("sys:eleEquipment:check")
	@RequestMapping(value = "check")
	public String check(String id, String delFlag, RedirectAttributes redirectAttributes){
		EleEquipment equipment = new EleEquipment(id);
		equipment.setDelFlag(delFlag);
		equipment.setCheckDate(new Date());
		equipment.setCheckBy(UserUtils.getUser());
		Result result = eleEquipmentService.checkEquipment(equipment);
		addMessage(redirectAttributes, result.getMsg());
		return "redirect:" + adminPath + "/equipment/eleEquipment/info?id=" + id;
	}
}